// vim: set et sw=4 ts=4 sts=4 fdm=marker ff=unix fenc=utf8 nobomb:
/**
 * Twitter Client Library
 *
 * @author mingcheng<i.feelinglucky@gmail.com>
 * @date   2009-11-14
 * @link   http://www.gracecode.com/
 */

/**
 * Twitter Request API
 */
Twitter = (function() {
    // default configure
    var config = {
        api: "http://twitter.com",
        username: "",
        password: "",
        timeout: 10000,
        count: 200
    };

    /**
     * sendRequest with YUI Connect Module
     */
    var sendRequest = function (request, callback) {
        var params = []; request.params = request.params || {};
        for(name in request.params) {
            params.push(name + '=' + encodeURIComponent(request.params[name]));
        }

        var request = YAHOO.util.Connect.asyncRequest(request.type || "GET", request.url, {
            success: callback.onSuccess || function () {},
            failure: callback.onError || function() {},
            timeout: config.timeout,
            cache: false
        }, params.join('&'), config.username, config.password);
    };

    //
    // Twitter request methods
    
    var sendDirectMessage = function(user, tweets, callback) {
        sendRequest({
            type: "POST", url: config.api + '/direct_messages/new.json',
            params: {
                'user': user,
                'text': tweets
            }
        }, callback);
    }

    var getFriendsTimeline = function(callback) {
        sendRequest({
            //url: config.api + '/statuses/friends_timeline.json?count=' + config.count
            url: "http://localhost/chrome-metrist/friends.json"
        }, callback);
    };

    var getDirectMessages = function(callback) {
        sendRequest({
            //url: config.api + '/direct_messages.json?count=' + config.count
            url: "http://localhost/chrome-metrist/direct.json"
        }, callback);
    }

    var getUserTimeline = function(user, callback) {
        sendRequest({
            url: config.api + '/statuses/user_timeline' + (user ? '/' + user : '') + '.json?count=' + config.count
        }, callback);
    }

    var getHomeTimeline = function(callback) {
        sendRequest({
            //url: config.api + '/statuses/home_timeline.json?count=' + config.count
            url: "http://localhost/chrome-metrist/home.json"
        }, callback);
    }

    var getFavorite = function(callback) {
        sendRequest({
            //url: config.api + '/favorites.json?count=' + config.count
            url: "http://localhost/chrome-metrist/favourite.json"
        }, callback);
    }

    var getReplies = function (callback) {
        sendRequest({
            //url: config.api + '/statuses/mentions.json?count=' + config.count
            url: "http://localhost/chrome-metrist/mentions.json"
        }, callback);
    }

    var getMentions = function (callback) {
        sendRequest({
            //url: config.api + '/statuses/mentions.json?count=' + config.count
            url: "http://localhost/chrome-metrist/mentions.json"
        }, callback);
    }

    var login = function (callback) {
        sendRequest({
            url: config.api + '/account/verify_credentials.json'
        }, callback);
    }

    var destroy = function(id, callback) {
        sendRequest({
            type: "POST",
            url: config.api + '/statuses/destroy/' + id + '.json'
        }, callback);
    }

    var update = function (tweets, callback) {
        sendRequest({
            type: "POST", url: config.api + '/statuses/update.json',
            params: {
                'status': tweets.status,
                'source': "<a href=\"http:\/\/lab.gracecode.com\/metrist\/\">Metrist<\/a>",
                'in_reply_to_user_id': tweets.in_user || '',
                'in_reply_to_status_id': tweets.in_status || ''
            }
        }, callback);
    }

    var favCreate = function(id, callback) {
        //POST
        //http://twitter.com/favorites/create/id.json
    }

    var favDestroy = function(id, callback) {
        //POST
        //http://twitter.com/favorites/destroy/id.json
    }


    // public method
    return {
        destroy: destroy,
        getDirectMessages: getDirectMessages,
        getFavorite: getFavorite,
        getFriendsTimeline: getFriendsTimeline,
        getHomeTimeline: getHomeTimeline,
        getMentions: getMentions,
        getReplies: getReplies,
        getUserTimeline: getUserTimeline,
        login: login,
        sendDirectMessage: sendDirectMessage,
        setConfig: function(user_config) {
            YAHOO.lang.augmentObject(config, user_config, true);
        },
        update: update
    };
})();
